package com.dnzx.controller.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.dnzx.framework.page.PageResult;
import org.dnzx.framework.result.ResultUtils;
import org.dnzx.framework.result.domain.Result;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dnzx.model.CartBO;
import com.dnzx.service.ICartService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("/web/cart")
@Api(value = "/web/cart",description="订单",produces=MediaType.APPLICATION_JSON_VALUE)
public class WebCartController {

	@Resource
	private ICartService cartService;

	/**
	 * 下单
	 * @param cartBO
	 * @return
	 */
	@PostMapping(value = "/save")
    @ApiOperation(value = "/save",notes = "新增",httpMethod = "POST",produces = MediaType.APPLICATION_JSON_VALUE)
    public Result save(@ApiParam(value = "实体") @RequestBody @Valid CartBO cartBO, HttpServletRequest request) {
		try{
			this.cartService.save(cartBO);
	        return ResultUtils.returnSuccess("SUCCESS");
        }catch (Exception e){
        	e.printStackTrace();
            return ResultUtils.returnError(e.getMessage());
        }
    }
	
	/**
	 * 查看
	 * @param id
	 * @return
	 */
	@GetMapping("/get/{id}")
	@ApiOperation(value = "/get/{id}",notes = "查看",httpMethod = "GET",produces=MediaType.APPLICATION_JSON_VALUE)
	public Result get(@ApiParam(value = "id") @PathVariable("id") Long id, HttpServletRequest request) {
		try {
			CartBO cartBO = this.cartService.get(id);
			return ResultUtils.returnSuccess("SUCCESS", cartBO);
		}catch(Exception e) {
			return ResultUtils.returnError(e.getMessage());
		}
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete/{id}")
	@ApiOperation(value = "/delete/{id}",notes = "删除",httpMethod = "DELETE",produces = MediaType.APPLICATION_JSON_VALUE)
	public Result delete(@ApiParam(value = "id") @PathVariable("id") Long id, HttpServletRequest request){
		try {
			this.cartService.delete(id);
			return ResultUtils.returnSuccess("SUCCESS");
		}catch(Exception e) {
			return ResultUtils.returnError(e.getMessage());
		}
	}

	/**
	 * 修改
	 * @param cartBO
	 * @return
	 */
	@PutMapping(value = "/update")
	@ApiOperation(value = "/update",notes = "修改",httpMethod = "PUT",produces = MediaType.APPLICATION_JSON_VALUE)
	public Result update(@ApiParam(value = "实体") @RequestBody CartBO cartBO, HttpServletRequest request) {
		try {
			this.cartService.update(cartBO);
			return ResultUtils.returnSuccess("SUCCESS");
		}catch(Exception e) {
			return ResultUtils.returnError(e.getMessage());
		}
	}


	/**
	 * 查询 - 分页
	 * @param cartBO
	 * @return
	 */
	@PostMapping("/queryPage")
	@ApiOperation(value="/queryPage",notes="查询",httpMethod="POST",produces=MediaType.APPLICATION_JSON_VALUE)
	public Result queryPage(@ApiParam(value = "实体") @RequestBody CartBO cartBO, HttpServletRequest request) {
		try {
			PageResult<List<CartBO>> pageResult = new PageResult<List<CartBO>>();
			pageResult = this.cartService.queryPage(cartBO, pageResult);
			return ResultUtils.returnSuccess("SUCCESS", pageResult);
		}catch(Exception e) {
			e.printStackTrace();
			return ResultUtils.returnError(e.getMessage());
		}
	}
	
	/**
	 * 查询 - 分页
	 * @param cartBO
	 * @return
	 */
	@PostMapping("/queryPageBuy")
	@ApiOperation(value="/queryPageBuy",notes="查询",httpMethod="POST",produces=MediaType.APPLICATION_JSON_VALUE)
	public Result queryPageBuy(@ApiParam(value = "实体") @RequestBody CartBO cartBO, HttpServletRequest request) {
		try {
			PageResult<List<CartBO>> pageResult = new PageResult<List<CartBO>>();
			pageResult = this.cartService.queryPage(cartBO, pageResult);
			return ResultUtils.returnSuccess("SUCCESS", pageResult);
		}catch(Exception e) {
			e.printStackTrace();
			return ResultUtils.returnError(e.getMessage());
		}
	}
	
	/**
	 * 查询 - 不分页
	 * @param cartBO
	 * @return
	 */
	@PostMapping("/queryList")
	@ApiOperation(value="/queryList",notes="查询 - 不分页",httpMethod="POST",produces=MediaType.APPLICATION_JSON_VALUE)
	public Result queryList(@ApiParam(value = "实体") @RequestBody CartBO cartBO, HttpServletRequest request) {
		try {
			List<CartBO> cartBOList = this.cartService.queryList(cartBO);
			return ResultUtils.returnSuccess("SUCCESS", cartBOList);
		}catch(Exception e) {
			e.printStackTrace();
			return ResultUtils.returnError(e.getMessage());
		}
	}

}
